<?php
declare (strict_types = 1);

namespace app\admin\controller;


use app\BaseController;
use app\model\Proceeds as ProceedsModel;
use app\model\ProceedsDetails;
use app\model\RefundDetails;
use think\Request;
use app\model\Refund as RefundModel;
class Refund extends BaseController
{
    public function index($limit = 10)
    {
        $name = request()->param('name');
        $status = request()->param('status');
        $time = request()->param('time');
        $school = request()->param('school_id');
        $data = RefundModel::with(['school','invoice','student','payment'])->where('school_id', $school)->when($name, function ($query) use ($name) {
            $query->where('student_name', 'like', "%$name%");
        })->when($status, function ($query) use ($status) {
            $query->where('status', $status);
        })->when($time, function ($query) use ($time) {
            $query->whereBetween('create_time', $time);
        })->order('id', 'desc')->paginate($limit)->toArray();
        return $this->success($data);
    }

    public function create()
    {
        $params = request()->param();
        $params['number'] = getNumber('退款');
       $ref= RefundModel::create($params);

//        1产品，2产品组合，3课程，4套票',
        $data=json_decode($params['item'],true);
        foreach ($data['course'] as $item){
            RefundDetails::create([
                'cid'=>$item['cid'],
                'type'=>3,
                'name'=>$item['name'],
                'money'=>$item['refund'],
                'refund_id'=>$ref->id
            ]);
        }
        foreach ($data['product'] as $item){
            RefundDetails::create([
                'cid'=>$item['id'],
                'type'=>1,
                'name'=>$item['name'],
                'money'=>$item['refund'],
                'refund_id'=>$ref->id
            ]);
        }
        foreach ($data['products_group'] as $item){
            RefundDetails::create([
                'cid'=>$item['id'],
                'type'=>2,
                'name'=>$item['name'],
                'money'=>$item['refund'],
                'refund_id'=>$ref->id
            ]);
        }
        foreach ($data['ticket'] as $item){
            RefundDetails::create([
                'cid'=>$item['id'],
                'type'=>4,
                'name'=>$item['name'],
                'money'=>$item['refund'],
                'refund_id'=>$ref->id
            ]);
        }
        return $this->success();
    }

    public function update($id)
    {
        $param=\request()->param();
        RefundModel::where('id', $id)->update([
            'school_id'=>$param['school_id'],
            'invoice_id'=>$param['invoice_id'],
            'student_name'=>$param['student_name'],
            'student_id'=>$param['student_id'],
            'payment'=>$param['payment'],
            'refund'=>$param['refund'],
            'item'=>$param['item'],
            'remark'=>$param['remark']
        ]);
        $params=request()->param();
        $data=json_decode($params['item'],true);

        RefundDetails::where('refund_id',$id)->delete();
        foreach ($data['course'] as $item){
            RefundDetails::create([
                'cid'=>$item['cid'],
                'type'=>3,
                'name'=>$item['name'],
                'money'=>$item['refund'],
                'refund_id'=>$id
            ]);
        }
        foreach ($data['product'] as $item){
            RefundDetails::create([
                'cid'=>$item['cid'],
                'type'=>1,
                'name'=>$item['name'],
                'money'=>$item['refund'],
                'refund_id'=>$id
            ]);
        }
        foreach ($data['products_group'] as $item){
            RefundDetails::create([
                'cid'=>$item['cid'],
                'type'=>2,
                'name'=>$item['name'],
                'money'=>$item['refund'],
                'refund_id'=>$id
            ]);
        }
        foreach ($data['ticket'] as $item){
            RefundDetails::create([
                'cid'=>$item['cid'],
                'type'=>4,
                'name'=>$item['name'],
                'money'=>$item['refund'],
                'refund_id'=>$id
            ]);
        }
        return $this->success();
    }

    public function delete($id)
    {
        RefundModel::where('id', 'in', $id)->delete();
        RefundDetails::where('refund_id','in',$id)->delete();
        return $this->success();
    }

    public function derive($limit=15)
    {
        $student_id = request()->param('student_id');
        $status = request()->param('status');
        $time = request()->param('time');
        $school = request()->param('school_id');
        $data = RefundModel::with(['school','invoice','student','payment'])
            ->when($student_id, function ($query) use ($student_id) {
            $query->where('student_id', $student_id);
        })     ->when($school, function ($query) use ($school) {
                $query->where('school_id', $school);
            })
            ->when($status, function ($query) use ($status) {
            $query->where('status', $status);
        })->when($time, function ($query) use ($time) {
            $query->whereBetween('create_time', $time);
        })->order('id', 'desc')->paginate($limit)->toArray();
        return $this->success($data);
    }

    public function derivepro($limit=15)
    {
        $student_id = request()->param('student_id');
        $status = request()->param('status');
        $time = request()->param('time');
        $school_id = request()->param('school_id');
        $admin_id = request()->param('admin_id');
       $data=RefundDetails::alias('rfd')
            ->join('refund rf','rf.id=rfd.refund_id','left')
            ->join('invoice inv', 'rf.invoice_id=inv.id', 'left')
            ->join('student stu','rf.student_id=stu.id','left')
//          ->join('proceeds_details pd','invd.id=pd.invoice_details_id','left')
            ->join('school sh','sh.id=rf.school_id','left')
           ->field('rfd.*,rf.number,inv.number invoice_number,stu.name student_name,stu.number student_number,sh.name school_name')
           ->when($time,function ($query)use($time){
                $query->whereBetween('rfd.create_time', $time);
            })->when($school_id,function($query) use($school_id){
                $query->where('rf.school_id',$school_id);
            })
            ->when($status,function($query) use($status){
                $query->where('rf.status',$status);
            })
            ->when($student_id,function($query) use($student_id){
                $query->where('rf.student_id',$student_id);
            })
            ->when($admin_id,function($query) use($admin_id){
                $query->where('rf.admin_id',$admin_id);
            })->paginate($limit);
        return $this->success($data);
    }
}
